package leetcode.code2241;

public class ATM {

	long[][] counts;

	public ATM() {
		this.counts = new long[5][2];
		this.counts[0][1] = 20;
		this.counts[1][1] = 50;
		this.counts[2][1] = 100;
		this.counts[3][1] = 200;
		this.counts[4][1] = 500;
	}

	public void deposit(int[] banknotesCount) {
		for (int i = 0; i < banknotesCount.length; i++) {
			this.counts[i][0] += banknotesCount[i];
		}
	}

	public int[] withdraw(int amount) {
		int[] ans = new int[5];
		long size = 0, money = 0;
		for (int i = 4; i >= 0 && amount > 0; i--) {
			if ((size = this.counts[i][0]) != 0) {
				long take = Math.min(size, amount / (money = counts[i][1]));
				amount -= take * money;
				ans[i] = (int) take;
			}
		}
		if (amount == 0) {
			for (int i = 0; i < ans.length; i++) {
				this.counts[i][0] -= ans[i];
			}
			return ans;
		} else {
			return new int[] { -1 };
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
